import os
import const
import numpy as np
from utils import file_util
from service import mongo_service

def load_dataset(username):
    npy_path = const.NPY_PATH + username + '/'
    file_util.no_exist_and_create(npy_path)
    embeddings = load_npy_file(username, npy_path)
    return embeddings

def load_npy_file(username, npy_path):
    filename = username + '.npy'
    filepath = npy_path + filename
    # get file info to MongoDB
    client, file_info = mongo_service.get_file_info(filename, 'embbing')
    remote_md5 = file_info._file['md5']
    if os.path.exists(filepath):
        # verify the md5
        local_md5 = file_util.get_md5(filepath)
        if remote_md5 == local_md5:
            return np.load(filepath)
    # download new file
    mongo_service.download_file(filepath, file_info)
    client.close()
    return np.load(filepath)
